package com.gouqi.mall.controller;

import com.gouqi.mall.domain.Suggestion;
import com.gouqi.mall.service.SuggestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Api(basePath = "/api/v1", value = "意见接口", description = "用户有什么意见可以提在系统上")
@Component
@RestController
@RequestMapping(value = "/suggest")
public class SuggestionController {

    @Autowired
    @Qualifier("suggestionServiceImpl")
    private SuggestionService suggestionService;

    @ResponseBody
    @RequestMapping(value = "/addOpinion.do",method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "添加意见",notes = "用户给系统提意见",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "token",value = "用户token",required = true,dataType = "String")
    })
    public String addSuggestion(@RequestHeader String token,@RequestBody @Valid Suggestion suggestion){
        JSONObject jsonObject = new JSONObject();
        System.out.println("提交的意见："+suggestion.toString());
        Integer result = suggestionService.addSuggestion(suggestion);
        if (result>0) {
            jsonObject.put("status",200);
            jsonObject.put("message","您的建议发送成功！");
        } else {
            jsonObject.put("status",404);
            jsonObject.put("message","您的建议发送失败！");
        }
        return jsonObject.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/getAllOpinions.do",method = RequestMethod.GET, produces = "application/json")
    @ApiOperation(value = "获取意见",notes = "获取用户给系统提的意见",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "token",value = "用户token",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "indexPage",value = "第几页",required = true,dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "perPage",value = "每页显示多少条",required = true,dataType = "int"),
    })
    public String getAllSuggestions(@RequestParam Integer indexPage,@RequestParam Integer perPage){
        JSONObject jsonObject = new JSONObject();
        System.out.println("分页查询建议结果集：");
        List<Suggestion> result = suggestionService.getAllSuggestions(indexPage,perPage);
        for (int i = 0; i < result.size(); i++) {
            System.out.println("第"+i+"条建议："+result.get(i).toString());
        }
        System.out.println("统计建议表条数：");
        Integer total = suggestionService.countSuggestions();
        //余数
        Integer remainder = total%perPage;
        //商
        Integer quotient = total/perPage;
        Integer pageCount =  remainder == 0 ? quotient:quotient+1;
        System.out.println("remainder余数:"+remainder+"  quotient商:"+quotient);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("total",total);
        map.put("index",indexPage);
        map.put("pageSize",perPage);
        map.put("pageCount",pageCount);
        map.put("result",result);
        if (result.size()>0) {
            jsonObject.put("status",200);
            jsonObject.put("message","查询建议成功！");
            jsonObject.put("data",map);
        } else {
            jsonObject.put("status",404);
            jsonObject.put("message","没有查询到建议！");
            jsonObject.put("data","");
        }
        return jsonObject.toString();
    }
}
